<?php

namespace app\api\controller\v1\disclose;

use app\model\disclose\Disclose;
use app\model\disclose\DiscloseRecord;
use app\model\disclose\DiscloseStaff;
use app\model\disclose\DiscloseType;
use app\model\staff\Staff;
use app\Request;

class DiscloseController
{
    public function index(Request $request)
    {
        list($status, $page) = $request->getMore([
            ['status', 1],
            ['page', 1],
        ], true);

        $where[] = ['disclose_staff.status', '=', 1];
        $where[] = ['complete_status', '=', $status];
        $where[] = ['disclose_staff.staff_id', '=', $request->staffId()];
        $list = DiscloseStaff::where($where)

            ->withJoin('disclose')
            ->page($page, 10)->order('id desc')
            ->select()->each(function ($data) {
                $data['type_name'] = DiscloseType::getOneName($data->disclose->type_id, 'type_name');
                $data['title'] = $data->disclose->project_name ;
                $data['id'] = $data->disclose->id ;
                $data['disclose_time'] = $data->disclose->disclose_time ;
                $data['type'] = $data->disclose->type ;
                unset($data->disclose) ;
            });


        return app('json')->success([
            'list' => $list,
        ]);
    }

    public function detail(Request $request)
    {
        list($id) = $request->getMore([
            ['id', 1],
        ], true);
        $info = Disclose::where('id', $id)->find();

        $where[] = ['status', '=', 1];
        $where[] = ['disclose_id', '=', $id];
        $where[] = ['staff_id', '=', $request->staffId()];
        $discloseStaff = DiscloseStaff::where($where)
            ->find();
        $typeName = DiscloseType::getOneName($info['type_id'], 'type_name');
//        $staffName = Staff::getOneName($info['staff_id'] , 'username');
//        $staffNames = Staff::getOneName($info['staff_id'] , 'username');

        return app('json')->success([
            'disclose_staff_id' => $discloseStaff['id'],
            'content' => [
                ['name' => '交底类型', 'con' => $typeName],
                ['name' => '项目名称', 'con' => $info['project_name']],
                ['name' => '建设单位', 'con' => $info['construction_name']],
                ['name' => '监理单位', 'con' => $info['supervision_name']],
                ['name' => '施工单位', 'con' => $info['build_name']],
                ['name' => '交底时间', 'con' => $info['build_name']],
//                ['name' => '交底人' , 'con' => $staffName] ,
//                ['name' => '被交底人' , 'con' => $info['build_name']] ,
                ['name' => '工程概况', 'con' => $info['overview']],
                ['name' => '现场风险', 'con' => $info['risk']],
                ['name' => '交底内容', 'con' => $info['content']],
                ['name' => '备注', 'con' => $info['remark']],
            ],
        ]);
    }

    public function submit(Request $request)
    {
        list($id, $imgUrl, $snapImgUrl) = $request->getMore([
            ['id', 1],
            ['img_url', ''],
            ['snap_img_url', ''],
        ], true);
        $discloseStaff = DiscloseStaff::where('disclose_id', $id)->where('staff_id', $request->staffId())->find();
        if ($discloseStaff->complete_status == 9) {
            return app('json')->fail('当前交底已完成');
        }
        if ($imgUrl == '') {
            return app('json')->fail('请先签字');
        }
        $discloseStaff->complete_time = time();
        $discloseStaff->snap_img_url = $snapImgUrl;
        $discloseStaff->img_url = $imgUrl;
        $discloseStaff->complete_status = 9;
        $discloseStaff->save();
        return app('json')->success('操作成功', []);
    }

    public function record(Request $request)
    {
        list($id) = $request->getMore([
            ['id', 1],
        ], true);
        $list = DiscloseRecord::where('disclose_id', $id)->order('id desc')->select();
        foreach ($list as $k => $val) {
            $list[$k]['img_urls'] = $val['img_urls'] != '' ? explode(',', $val['img_urls']) : [];
            $list[$k]['video_urls'] = $val['video_urls'] != '' ? explode(',', $val['video_urls']) : [];
        }
        return app('json')->success(['list' => $list]);

    }

    public function dynamics_save(Request $request)
    {
        list($id, $imgUrls, $content, $videoUrls) = $request->postMore([
            ['id', 1],
            ['img_urls', '', 'trim'],
            ['content', ''],
            ['video_urls', ''],
        ], true);

        $model = new DiscloseRecord();

        $imgUrls = $request->param('img_urls');
        if ($content == '' && $imgUrls == '') {
            return app('json')->fail('请输入备注内容或者上传照片');
        }
        $model->save([
            'disclose_id' => $id,
            'img_urls' => $imgUrls,
            'content' => $content,
            'video_urls' => $videoUrls,
            'staff_id' => $request->staffId(),
        ]);
        return app('json')->success('操作成功222');

    }

}